using UnityEngine;
using System.Collections;

public class Healthbar : MonoBehaviour {

	public Transform target;

	Vector3 offset = new Vector3(0, 0.9f, 0);

	float baseScale = 1.2f;

	// Update is called once per frame
	void Update () {
		if(target==null) {
			Destroy(gameObject);
			return;
		}

		transform.position = target.position + offset;

		Health h = target.GetComponent<Health>();
		float perc = h.hps / h.initHPs;

		if(perc >= 1) {
			GetComponent<SpriteRenderer>().enabled = false;
		}
		else {
			GetComponent<SpriteRenderer>().enabled = true;
			Vector3 s = transform.localScale;
			s.x = baseScale * perc;
			transform.localScale = s;
		}
	}
}
